package ims.servlet;

import ims.beans.Product;
import ims.beans.ProductType;
import ims.constants.IMSConstants;
import ims.db.view.prodType.ViewProdTypesDAO;
import ims.db.view.prodType.ViewProdTypesDbRequest;
import ims.db.view.prodType.ViewProdTypesDbResponse;
import ims.db.view.product.ViewProductDAO;
import ims.db.view.product.ViewProductDbRequest;
import ims.db.view.product.ViewProductDbResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ProdTypeServlet
 */
@WebServlet("/ProdTypeServlet")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			response.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
 
			//Retrieve all product types
			ViewProductDbRequest dbRequest = new ViewProductDbRequest();
			dbRequest.setViewAll(true);
			ViewProductDAO db = new ViewProductDAO();
			ViewProductDbResponse dbResponse = (ViewProductDbResponse) db.execute(dbRequest);
			ArrayList<Product>products = dbResponse.getProducts();
			
			
			//generating output of page
			out.println("<html>");
            out.println("<head>");
            out.println("<title>Product</title>");
            out.println("</head>");
            out.println("<a href='/IMS/prodType'>Product Type</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/searchCustOrder'>Search Customer Order</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/account.jsp'>Update Accounts</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/reports.jsp'>Reports</a></br></br>");
            out.println("<h1>Product</h1>");
            out.println("<body>");
            //add
            out.println("<form name='addProductForm' action='/IMS/addProduct' method='get'>" );
            out.println("Product: <input type='text' name='prodNm' /> <input type='submit' value='Add Product' /> ");
            out.println(" </form> " );
            
            //delete
            out.println("<form name='deleteProductForm' action='/IMS/deleteProduct' method='post'>" );
            out.println("Product: <select name='prodName'>");
            for(int i=0; i<products.size();i++)
            {
            	Product product = products.get(i);
            	out.println("<option value='" +product.getPid()+ ":"+ product.getProdDesc()+"'>" + product.getProdDesc()+"</option>" );
            			
            }
            out.println("</select>");
            out.println("<button type='submit' name='Delete' >Delete</button>");
            out.println(" </form> " );
            
            //update
            out.println("<form name='updateProductForm' action='/IMS/product' method='post'>" );
            out.println("Product: <select name='product'>");
            for(int i=0; i<products.size();i++)
            {
            	Product product = products.get(i);
            	out.println("<option value='" +product.getPid()+ "'>" + product.getProdDesc()+"</option>" );
            			
            }
            out.println("</select>");
            out.println("<button type='submit' name='Update' >Update</button>");
            out.println(" </form> " );
            String respMsg = (String)request.getParameter(IMSConstants.RESPONSE_HEADER);
            if (respMsg!=null)
            	out.println(respMsg);
            
            //list of all products
            out.println("<table border='1'><tr><th>#</th><th>Product Type</th><th>Product</th><th>Price</th></tr>");
            
            for(int i=0; i<products.size();i++)
            {
            	out.println("<tr>");
            	Product product = products.get(i);
            	int displayCtr = i+1;
            	out.println("<td>" +displayCtr+ "</td>");
            	out.println("<td>" +product.getProdType()+ "</td>");
            	out.println("<td>" +product.getProdDesc()+ "</td>");
            	out.println("<td>" +product.getPrice()+ "</td>");
            	out.println("</tr>");
            }
            
            out.println("</body>");

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			String prodId = request.getParameter("product");
	
			//Retrieve product type
			ViewProductDbRequest dbRequest = new ViewProductDbRequest();
			dbRequest.setViewAll(false);
			dbRequest.setProdId(prodId);
			ViewProductDAO db = new ViewProductDAO();
			ViewProductDbResponse dbResponse = (ViewProductDbResponse) db.execute(dbRequest);
			ArrayList<Product>products = dbResponse.getProducts();
			Product product = products.get(0);
			
			response.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
	        out.println("<html>");
            out.println("<head>");
            out.println("<title>Update Product</title>");
            out.println("</head>");
            out.println("<body>");
            StringBuffer form = new StringBuffer();
            form.append("<form name='updateProductForm' action='/IMS/updateProduct?prodId=");
            form.append(prodId).append("' method='post'>");
            out.println(form.toString());
            StringBuffer prod = new StringBuffer();
            prod.append("Product name: <input type='text' name='prodNm' size=40 maxlength=40 ");
            prod.append("value='").append(product.getProdDesc()).append("' /></br>");
            out.println(prod.toString());
            
            //Retrieve all product types
			ViewProdTypesDbRequest viewdbRequest = new ViewProdTypesDbRequest();
			viewdbRequest.setViewAll(true);
			ViewProdTypesDAO viewdb = new ViewProdTypesDAO();
			ViewProdTypesDbResponse viewdbResponse = (ViewProdTypesDbResponse) viewdb.execute(viewdbRequest);
			ArrayList<ProductType>prodTypes = viewdbResponse.getProdTypes();
			//Product Type field
			out.println("Product Type: <select name='prodType'>");
            for(int i=0; i<prodTypes.size();i++)
            {
            	ProductType prodType = prodTypes.get(i);
            	if (product.getProdTypeId().equals(prodType.getProdTypeId()))
            		out.println("<option value='" +prodType.getProdTypeId()+"' selected>" + prodType.getProdType()+"</option>" );
            	else
            		out.println("<option value='" +prodType.getProdTypeId()+"'>" + prodType.getProdType()+"</option>" );
            }
            out.println("</select> </br>");
            
            out.println("Price: <input type='text' name='price' value='"+product.getPrice()+"' /></br>");
            out.println("<button type='submit' name='Update'>Update</button>");
            out.println(" </form> " );
            out.println("</body>");
            
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
